package com.arvts.android.project.data;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.arvts.android.project.model.Vehicle;

public class VehicleDS {

	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	
	public VehicleDS(Context context) {
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}

	public void open() throws SQLException {
		dB = dbHelper.getWritableDatabase();
	}

	
	@SuppressWarnings("static-access")
	public int createOrUpdateArea(ArrayList<Vehicle>  list) {
		
		int count =0;
		
		deleteAll();
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{
		
    for (Vehicle vehicle : list) {
    		ContentValues values = new ContentValues();
    		values.put(dbHelper.VEHICLE_ID, vehicle.getVEHICLE_ID());
			values.put(dbHelper.VEHICLE_SERVICE_DURATION, vehicle.getVEHICLE_SERVICE_DURATION());
			values.put(dbHelper.VEHICLE_IDLE_TIME_LIMIT, vehicle.getVEHICLE_IDLE_TIME_LIMIT());
			values.put(dbHelper.VEHICLE_NUMBER, vehicle.getVEHICLE_NUMBER());
			values.put(dbHelper.VEHICLE_DEVICE_PASSWORD, vehicle.getVEHICLE_DEVICE_PASSWORD());
			values.put(dbHelper.VEHICLE_TYPE, vehicle.getVEHICLE_TYPE());
			values.put(dbHelper.VEHICLE_LOCK_STATUS, vehicle.getVEHICLE_LOCK_STATUS());
			values.put(dbHelper.VEHICLE_LOCKED_DATE, vehicle.getVEHICLE_LOCKED_DATE());
			values.put(dbHelper.VEHICLE_DATE_CREATED, vehicle.getVEHICLE_DATE_CREATED());
			values.put(dbHelper.VEHICLE_SECONDARY_PHONE_NUMBER, vehicle.getVEHICLE_SECONDARY_PHONE_NUMBER());
			values.put(dbHelper.VEHICLE_SERVICE_MILEAGE, vehicle.getVEHICLE_SERVICE_MILEAGE());
			values.put(dbHelper.VEHICLE_INITIAL_MILEAGE_READING, vehicle.getVEHICLE_INITIAL_MILEAGE_READING());
			values.put(dbHelper.VEHICLE_DEVICE_PHONE_NUMBER, vehicle.getVEHICLE_DEVICE_PHONE_NUMBER());
			
			count = (int) dB.insert(dbHelper.TABLE_VEHICLE, null, values);

			}
		}catch (Exception e) {
		
			Log.v("Exception", e.toString());
	
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return count;
				
	}
	
	public int deleteAll(){
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		try{
			
			cursor = dB.rawQuery("SELECT * FROM " + dbHelper.TABLE_VEHICLE, null);
			count =cursor.getCount();
			if(count>0){
				int success = dB.delete(dbHelper.TABLE_VEHICLE, null, null);
				Log.v("Success", success+"");
			}
		}catch (Exception e){

			Log.v("Exception", e.toString());
		}finally{  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		
		return count;
		
	}
	
	
	public ArrayList<Vehicle> getAllVehicleDetails() {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<Vehicle> list = new ArrayList<Vehicle>();
		
		String selectQuery = "select * from "+dbHelper.TABLE_VEHICLE;
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		while(cursor.moveToNext()){
			
			Vehicle vehicle=new Vehicle();
		
			vehicle.setVEHICLE_ID(cursor.getString(cursor.getColumnIndex(dbHelper.VEHICLE_ID)));
			vehicle.setVEHICLE_SERVICE_DURATION(cursor.getString(cursor.getColumnIndex(dbHelper.VEHICLE_SERVICE_DURATION)));
			vehicle.setVEHICLE_IDLE_TIME_LIMIT(cursor.getString(cursor.getColumnIndex(dbHelper.VEHICLE_IDLE_TIME_LIMIT)));
			vehicle.setVEHICLE_NUMBER(cursor.getString(cursor.getColumnIndex(dbHelper.VEHICLE_NUMBER)));
			vehicle.setVEHICLE_DEVICE_PASSWORD(cursor.getString(cursor.getColumnIndex(dbHelper.VEHICLE_DEVICE_PASSWORD)));
			vehicle.setVEHICLE_TYPE(cursor.getString(cursor.getColumnIndex(dbHelper.VEHICLE_TYPE)));
			vehicle.setVEHICLE_LOCK_STATUS(cursor.getString(cursor.getColumnIndex(dbHelper.VEHICLE_LOCK_STATUS)));
			vehicle.setVEHICLE_LOCKED_DATE(cursor.getString(cursor.getColumnIndex(dbHelper.VEHICLE_LOCKED_DATE)));
			vehicle.setVEHICLE_DATE_CREATED(cursor.getString(cursor.getColumnIndex(dbHelper.VEHICLE_DATE_CREATED)));
			vehicle.setVEHICLE_SECONDARY_PHONE_NUMBER(cursor.getString(cursor.getColumnIndex(dbHelper.VEHICLE_SECONDARY_PHONE_NUMBER)));
			vehicle.setVEHICLE_SERVICE_MILEAGE(cursor.getString(cursor.getColumnIndex(dbHelper.VEHICLE_SERVICE_MILEAGE)));
			vehicle.setVEHICLE_INITIAL_MILEAGE_READING(cursor.getString(cursor.getColumnIndex(dbHelper.VEHICLE_INITIAL_MILEAGE_READING)));
			vehicle.setVEHICLE_DEVICE_PHONE_NUMBER(cursor.getString(cursor.getColumnIndex(dbHelper.VEHICLE_DEVICE_PHONE_NUMBER)));

			
			list.add(vehicle);
			
		}
		
		return list;
	}
	
	
	@SuppressWarnings("static-access")
	public String getVehicleNumberByID(String sID) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
				
		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_VEHICLE +" WHERE "+dbHelper.VEHICLE_ID+"='"+sID+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){

			return cursor.getString(cursor.getColumnIndex(dbHelper.VEHICLE_NUMBER));
			
		}
		
		return "";
	}
	
}